using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;

public class SceneController : Singleton<SceneController>, IEndGameObserver
{
    public GameObject playerPrefab;
    public SceneFader sceneFaderPrefab;
    NavMeshAgent agent;
    GameObject player;
    bool fadeFinish;
    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(this);
    }
    void Start()
    {
        GameManager.Instance.AddEndGameObservers(this);
        fadeFinish = true;
    }
    public void TransitionToDestination(TransitionPoint transitionPoint)
    {
        switch (transitionPoint.transitionType)
        {
            case TransitionPoint.TransitionType.SameScene:
                StartCoroutine(
                    Transition(SceneManager.GetActiveScene().name, transitionPoint.destinationTag)
                );
                break;
            case TransitionPoint.TransitionType.DifferentScene:
                StartCoroutine(
                    Transition(transitionPoint.sceneName, transitionPoint.destinationTag)
                );
                break;
        }
    }
    // 触碰传送门
    IEnumerator Transition(string sceneName, DestinationTag destinationTag)
    {
        SaveSceneData();

        // 跨场景传送
        if (SceneManager.GetActiveScene().name != sceneName)
        {
            SceneFader fader = Instantiate(sceneFaderPrefab);
            // 淡出
            yield return StartCoroutine(fader.FadeOut(1f));

            yield return SceneManager.LoadSceneAsync(sceneName);

            var dest = GetDestination(destinationTag);
            yield return Instantiate(playerPrefab, dest.transform.position, dest.transform.rotation);
            // 加载玩家数据
            SaveManager.Instance.LoadPlayerData();

            // 加载背包数据，重新装备上次退出游戏时的装备
            InventoryManager.Instance.CheckEquipmentInPlayer();

            // 淡入
            yield return StartCoroutine(fader.FadeIn(1f));

            yield break;
        }
        // 同场景传送
        else
        {
            player = GameManager.Instance.playerStat.gameObject;
            agent = player.GetComponent<NavMeshAgent>();
            agent.enabled = false;
            var dest = GetDestination(destinationTag);
            player.transform.SetPositionAndRotation(dest.transform.position, dest.transform.rotation);
            agent.enabled = true;
            yield return null;
        }
    }
    public void TransitionToFirstScene()
    {
        StartCoroutine(Loadlevel("Game"));
    }
    IEnumerator Loadlevel(string sceneName)
    {
        if (sceneName != string.Empty)
        {
            SceneFader fader = Instantiate(sceneFaderPrefab);
            // 淡出
            yield return StartCoroutine(fader.FadeOut(2f));

            yield return SceneManager.LoadSceneAsync(sceneName);

            var entrance = GameManager.Instance.GetEntrance();
            yield return Instantiate(playerPrefab, entrance.position, entrance.rotation);
            // 进入新场景时，再次保存玩家数据
            SaveSceneData();
            // 淡入
            yield return StartCoroutine(fader.FadeIn(2f));
            yield break;
        }
    }
    private void SaveSceneData()
    {
        // 保存玩家数据
        SaveManager.Instance.SavePlayerData();
        // 保存背包数据
        InventoryManager.Instance.SaveData();
        // 保存任务数据
        QuestManager.Instance.SaveQuestSystem();
    }
    public void TransitionToLoadGame()
    {
        StartCoroutine(LoadGame());
    }
    IEnumerator LoadGame()
    {
        yield return Loadlevel(SaveManager.Instance.SceneName);
        // 如果装备格有武器，重新装备武器
        InventoryManager.Instance.CheckEquipmentInPlayer();
    }
    private TransitionDestination GetDestination(DestinationTag destinationTag)
    {
        var entrances = FindObjectsOfType<TransitionDestination>();
        foreach (var e in entrances)
        {
            if (e.destinationTag == destinationTag)
            {
                return e;
            }
        }
        return null;
    }
    public void TransitionToMain()
    {
        // TODO: 保存进度，卸载场景
        SaveSceneData();
        StartCoroutine(LoadMenu());
    }
    IEnumerator LoadMenu()
    {
        yield return SceneManager.LoadSceneAsync("MainMenu");
        yield break;
    }
    IEnumerator EndGame()
    {
        yield return new WaitForSeconds(2f);
        yield return LoadMenu();
    }
    public void EndNotify()
    {
        if (fadeFinish)
        {
            fadeFinish = false;
            StartCoroutine(EndGame());
        }
    }
}
